package net.tycmc.cty.ctyms.timejob.iems.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.MyBaseDAO;


public class FuelLevelMonitorDao {
	private IBaseDAO baseDAO;
	private ITransactionDAO tranDAO;
	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}

	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}
	/**获取燃油液位列表
	 * @param param
	 * @return
	 */
	public List<Map<String,Object>> getTimingInfoOfFuelLevel(Map<String,Object>param){
		String dateTime=MapGetter.getString(param,"dateTime").substring(0,10);
		StringBuffer str=new StringBuffer();
		str.append("SELECT tanklevel =msgt_tanklevel,");
		/*str.append(" CASE WHEN ExcursionA=1 OR ExcursionA IS NULL  OR ExcursionB=0 OR ExcursionB IS NULL THEN '--' ELSE CONVERT(VARCHAR(20),ExcursionA*msgt_tanklevel+ExcursionB,120) END ")*/
		str.append("tankchangestate = msgt_tankchangestate,");
		str.append("tankchangevalue=msgt_tankchangevalue,");
		/*str.append("tankchangevalue = CASE WHEN ExcursionA=1 OR ExcursionA IS NULL  OR ExcursionB=0 OR ExcursionB IS NULL THEN '0' ELSE CONVERT(VARCHAR(20),ExcursionA*msgt_tankchangevalue+ExcursionB,120) END ,");*/
		str.append("vehicleid,");
		str.append("msgt_id,msgt_tankchangestate,");
		str.append("msg_map = CASE ");
		str.append("WHEN msgt_lo IS NULL ");
		str.append("OR msgt_la IS NULL  THEN (SELECT   top 1   Convert(VARCHAR(20),msgt_lo,120) + ',' + Convert(VARCHAR(20),msgt_la,120)");
		 str.append("FROM "+GlobalDBVar.getDBFullName_Conn("msg_timing_"+dateTime.replaceAll("-","").substring(0,8),"4",dateTime) +"  ");
		 str.append("  WHERE   msgt_msgtime < m.msgt_msgtime");
		 str.append("	and MsgT_Vcl_ID=m.MsgT_Vcl_ID");
		 str.append(" AND msgt_lo IS NOT NULL ");
		 str.append("  AND msgt_la IS NOT NULL ");
		 str.append(" ORDER BY msgt_msgtime DESC )");
		 str.append("  else Convert(VARCHAR(20),msgt_lo,120)  + ',' + Convert(VARCHAR(20),msgt_la,120)  END,");
		 str.append("  msgt_msgtime");
		 str.append("   FROM  "+GlobalDBVar.getDBFullName_Conn("msg_timing_"+dateTime.replaceAll("-","").substring(0,8),"4",dateTime) +" AS m");
		 str.append("   INNER JOIN V_VehicleInfo");
		 str.append("   ON msgt_vcl_id = vehicleid");
		 str.append("  WHERE    msgt_tanklevel is not null and MsgT_MsgTime between '"+dateTime+"' and '"+dateTime+" 23:59:59' ");
		 /*String sql="select * from ("+str.toString()+")cc where tanklevel!='--' order by vehicleid,msgt_msgtime asc";*/
		 return baseDAO.execQuery(str.toString(),new Object[]{});
	}
	public String getdatePlusOneHour(String dateTime,int hours ) throws ParseException{
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(myFormatter.parse(dateTime));
		calendar.add(Calendar.HOUR_OF_DAY, hours);
		return myFormatter.format(calendar.getTime());
	}
	public boolean compareDate(String beginTime,String endTime,String dateTime) throws ParseException{
		SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date bgnTime=myFormatter.parse(beginTime);
		Date eTime=myFormatter.parse(endTime);
		Date dTime=myFormatter.parse(dateTime);
		System.out.println(bgnTime.getTime());
		System.out.println(dTime.getTime());
		System.out.println(eTime.getTime());
		if(bgnTime.getTime()<=dTime.getTime()&&eTime.getTime()>=dTime.getTime()){
			return true;
		}else{
			return false;
		}
	}
	
	/**2000条一次执行
	 * @param sqlList
	 * @return
	 */
	public boolean execBatchSql(List<String>sqlList){
		int size=sqlList.size();
		int perNum=2000;
		int execTime=size/perNum;
		int resultNum=1;
		int num=1;
		for(int i=0;i<execTime;i++){
			boolean flag=tranDAO.batch(sqlList.subList(i*perNum,(i+1)*perNum));
			if(flag==false){
				 num=0;
			}
			resultNum=resultNum*num;
		}
		int yushu=size%perNum;
		if(yushu>0){
			boolean flag1=tranDAO.batch(sqlList.subList(execTime*perNum,execTime*perNum+yushu));
			if(flag1==false){
				 num=0;
			}
			resultNum=resultNum*num;
		}
		return 0==resultNum?false:true;
	}
}
